package com.aliyun.heiyu.powermaster.server.rotary.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.proto.AlgorithmRequest;
import com.aliyun.heiyu.powermaster.proto.AlgorithmsRequest;
import com.aliyun.heiyu.powermaster.server.rotary.dao.AlgorithmDAO;
import com.aliyun.heiyu.powermaster.server.rotary.object.entity.AlgorithmEntity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: heiyu
 * @description: 算法接口实现
 * @author: zhangshun
 * @create: 2020-02-16 07:01
 **/
public class AlgorithmDAOImpl implements AlgorithmDAO {
    /**
     * 查询算法列表
     * @param request
     * @return
     */
    @Override
    public List<AlgorithmEntity> queryAlgorithms(AlgorithmsRequest request) {
        StringBuffer sql = new StringBuffer();
        sql.append("select id,algo_name,cooling_allowance,control_frequency,temp_precision,algo_switch from d_algorithm  where del = 1 ");
        List obj = new ArrayList();
        //策略名称
        if (!StringUtil.isEmpty(request.getAlgoName())) {
            sql.append(" and algo_name like ? ");
            obj.add("%"+request.getAlgoName()+"%");
        }
        //策略状态
        if(!StringUtil.isEmpty(request.getAlgoSwitch())){
            sql.append(" and algo_switch = ? ");
            obj.add(request.getAlgoSwitch());
        }
        sql.append(" order by update_time desc");
        Object[] objects = obj.toArray();
        return JdbcUtil.queryForList(sql.toString(), objects, AlgorithmEntity.class);
    }

    /**
     * 查询算法详情
     * @param id
     * @return
     */
    @Override
    public AlgorithmEntity queryAlgorithmById(String id) {
        StringBuffer sql = new StringBuffer();
        sql.append("select id,algo_name,cooling_allowance,control_frequency,temp_precision,algo_switch from d_algorithm  where id = ? and del = 1");
        Object[] obj = new Object[1];
        obj[0] = id;
        return JdbcUtil.queryForBean(sql.toString(), obj, AlgorithmEntity.class);
    }

    /**
     * 删除算法
     * @param id
     * @return
     */
    @Override
    public boolean deleteAlgorithm(String id) {
        StringBuffer sql = new StringBuffer();
        sql.append("update d_algorithm set del = 0 where id = ?");
        Object[] obj = new Object[1];
        obj[0] = id;
        return JdbcUtil.update(sql.toString(),obj);
    }

    /**
     * 修改算法
     * @param request
     * @return
     */
    @Override
    public boolean updateAlgorithm(AlgorithmRequest request) {
        String UPDATE_ALGORITHM = "UPDATE d_algorithm SET cooling_allowance=?,control_frequency=?,temp_precision=?,update_time=? WHERE id = ?";
        Object[] obj = new Object[5];
        obj[0] = request.getCoolingAllowance();
        obj[1] = request.getControlFrequency();
        obj[2] = request.getTempPrecision();
        obj[3] = new Date();
        obj[4] = request.getId();
        return JdbcUtil.update(UPDATE_ALGORITHM, obj);
    }

    /**
     * 添加算法
     * @param request
     * @return
     */
    @Override
    public int addAlgorithm(AlgorithmRequest request) {
        String ADD_ALGORITHM = "INSERT INTO d_algorithm(id,algo_name,cooling_allowance,control_frequency,temp_precision,algo_switch,del,create_time,update_time) VALUES(?,?,?,?,?,?,?,?,?)";
        Object[] obj = new Object[9];
        obj[0] = request.getId();
        obj[1] = request.getAlgoName();
        obj[2] = request.getCoolingAllowance();
        obj[3] = request.getControlFrequency();
        obj[4] = request.getTempPrecision();
        obj[5] = 0;
        obj[6] = 1;
        obj[7] = new Date();
        obj[8] = new Date();
        try {
            return JdbcUtil.insert(ADD_ALGORITHM,obj);
        }catch (Exception e){
            return 1;
        }
    }

    /**
     * 开启算法
     * @param request
     * @return
     */
    @Override
    public boolean updateAlgorithmSwitch(AlgorithmRequest request) {
        String sql = "update d_algorithm set algo_switch = 1 where id = ?";
        Object[] obj = new Object[1];
        obj[0] = request.getId();
        return JdbcUtil.update(sql, obj);
    }

    /**
     * 查询算法是否存在
     *
     * @param algoName
     * @return
     */
    @Override
    public AlgorithmEntity queryAlgoByName(String algoName) {
        String sql = "select id,algo_name from d_algorithm where del = 1 and algo_name = ?";
        Object[] obj = new Object[1];
        obj[0] = algoName;
        return JdbcUtil.queryForBean(sql,obj,AlgorithmEntity.class);
    }

    /**
     * 关闭所有算法
     *
     * @return
     */
    @Override
    public boolean closeAlgos() {
        String sql = "update d_algorithm set algo_switch = 0";
        return JdbcUtil.update(sql, null);
    }

    /**
     * 查询在使用的算法
     *
     * @return
     */
    @Override
    public AlgorithmEntity queryAlgoUse() {
        String sql ="select algo_name,cooling_allowance,control_frequency,temp_precision from d_algorithm where algo_switch = 1 and del= 1 limit 1";
        return JdbcUtil.queryForBean(sql,null,AlgorithmEntity.class);
    }

}
